package com.wangjiyuan.giftshuo.ui.fragment.other.detail;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshWebView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.base.BaseFragment;
import com.wangjiyuan.giftshuo.bean.detail.DataBean;
import com.wangjiyuan.giftshuo.bean.home.column.ItemsBean;
import com.wangjiyuan.giftshuo.ui.activity.other.more.MoreActivity;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DetailFragment extends BaseFragment implements View.OnClickListener, DetailContract.DetailView {
    private WebView refreshableView;

    private ImageView back;
    private TextView more;
    private XRecyclerView detail;
    private LinearLayout action_item;
    private TextView favourite;
    private TextView share;
    private TextView comment;
    private int id;
    private DetailPresenter presenter;
    private PullToRefreshWebView detail_web_view;
    private List<Integer> ids;
    private List<ItemsBean> items;

    public DetailFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        id = arguments.getInt("id");
        ids = (List<Integer>) arguments.getSerializable("ids");
        items = (List<ItemsBean>) arguments.getSerializable("items");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_detail, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        back = (ImageView) view.findViewById(R.id.back);
        more = (TextView) view.findViewById(R.id.more);
        action_item = (LinearLayout) view.findViewById(R.id.action_item);
        favourite = (TextView) view.findViewById(R.id.favourite);
        share = (TextView) view.findViewById(R.id.share);
        comment = (TextView) view.findViewById(R.id.comment);
        back.setOnClickListener(this);
        more.setOnClickListener(this);
        favourite.setOnClickListener(this);
        share.setOnClickListener(this);
        comment.setOnClickListener(this);

        detail_web_view = (PullToRefreshWebView) view.findViewById(R.id.detail_web_view);
        ILoadingLayout startLabels = detail_web_view.getLoadingLayoutProxy(true, false); //参数1=设置头部，参数2=设置尾部
        startLabels.setPullLabel("下拉刷新...");// 刚下拉时，显示的提示
        startLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示
        startLabels.setRefreshingLabel("正在载入...");// 刷新时
        //设置上拉刷新
        ILoadingLayout endLabels = detail_web_view.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("上拉刷新...");// 刚下拉时，显示的提示
        endLabels.setRefreshingLabel("正在载入...");// 刷新时
        endLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示

        detail_web_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<WebView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<WebView> refreshView) {
                int i = ids.indexOf(id);
                if (i != 0) {
                    id = ids.get(i - 1);
                    presenter.getData(String.valueOf(id));
                } else {
                    Toast.makeText(mContext, "没有了", Toast.LENGTH_SHORT).show();
                }
                refreshView.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<WebView> refreshView) {
                int i = ids.indexOf(id);
                if (i != ids.size()) {
                    id = ids.get(i + 1);
                    presenter.getData(String.valueOf(id));
                } else {
                    Toast.makeText(mContext, "没有了", Toast.LENGTH_SHORT).show();
                }
                refreshView.onRefreshComplete();
            }
        });
    }


    @Override
    public void onResume() {
        super.onResume();
        presenter = new DetailPresenter(this);
        presenter.getData(String.valueOf(id));

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.back:
                getActivity().finish();
                break;
            case R.id.more:
                intent.setClass(mContext, MoreActivity.class);
                int id = items.get(ids.indexOf(this.id)).getColumn().getId();
                intent.putExtra("id", id);
                startActivity(intent);
                break;
            case R.id.favourite:
                break;
            case R.id.share:
                break;
            case R.id.comment:
                break;
        }
    }

    @Override
    public void getData(DataBean bean) {

        refreshableView = detail_web_view.getRefreshableView();
        refreshableView.loadUrl(bean.getUrl());
        refreshableView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });
        favourite.setText(bean.getLikesCount() + "");
        share.setText(bean.getSharesCount() + "");
        comment.setText(bean.getCommentsCount() + "");
        detail_web_view.onRefreshComplete();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        refreshableView.stopLoading();
        ((ViewGroup)refreshableView.getParent()).removeView(refreshableView);
        refreshableView.removeAllViews();
        refreshableView.clearCache(true);
        refreshableView.clearHistory();
        refreshableView.destroy();
    }
}
